package com.lagou.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.blog.mapper.BlogMapper;
import com.lagou.blog.model.BlogPageInfo;
import com.lagou.blog.model.BlogRecord;
import com.lagou.blog.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 博客服务
 */
@Service("blogService")
public class BlogServiceImpl implements BlogService {
    /**
     * 博客信息的Mapper，用于访问数据库记录
     */
    @Autowired
    private BlogMapper blogMapper;

    /**
     * 查询博客信息
     *
     * @param page 要查询的页
     * @param pageSize 每页的大小
     * @return 博客信息
     */
    @Override
    public BlogPageInfo queryBlogs(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<BlogRecord> blogs = blogMapper.queryBlogs();

        PageInfo<BlogRecord> pageInfo = new PageInfo<>(blogs);
        BlogPageInfo blogPageInfo = new BlogPageInfo();

        blogPageInfo.setBlogs(blogs);
        blogPageInfo.setCurrentPage(page);
        blogPageInfo.setTotalPage(pageInfo.getPages());

        return blogPageInfo;
    }
}
